package com.cheetah.start.module.monitor.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.cheetah.start.module.monitor.domain.Cpu;
import com.cheetah.start.module.monitor.domain.Disk;
import com.cheetah.start.module.monitor.domain.Mem;
import com.cheetah.start.module.monitor.domain.Server;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 25175
 * @date 2022/4/2 10:55
 */
@Slf4j
@Service
public class SystemService {

    @Autowired
    private SendMsgService sendMsgService;
    @Autowired
    private DingDingService dingDingService;
    @Value("${spring.profiles.active}")
    private String active;

    /**
     * 获取系统信息
     *
     * @return {@link Server}
     * @throws Exception 异常
     */
    public Server getSystemInfo() {
        Server server = null;
        try {
            server = new Server();
            server.serverInfo();
        } catch (Exception e) {
            log.error("系统信息获取异常", e);
        }
        return server;
    }


    public void monitor() {
        Server systemInfo = getSystemInfo();
        Cpu cpu = systemInfo.getCpu();
        Mem mem = systemInfo.getMem();
        List<Disk> disks = systemInfo.getDisks();
        double cpuSys = cpu.getUsed() / 100;
        double memSys = mem.useRate();
        double diskSys = 0;
        if (CollUtil.isNotEmpty(disks)) {
            double totalAll = 0;
            double totalFree = 0;
            for (Disk disk : disks) {
                double total = disk.getTotalGb();
                totalAll += total;
                double freeGb = disk.getFreeGb();
                totalFree += freeGb;
            }
            diskSys = (totalAll - totalFree) / totalAll;
        }
        int i = 0;
        String cpu1 = extracted(cpuSys, "Cpu");
        String m = extracted(memSys, "内存");
        String d = extracted(diskSys, "硬盘");

        StringBuilder stringBuilder = new StringBuilder();
        if (StrUtil.isNotBlank(cpu1)) {
            i++;
            stringBuilder.append(cpu1).append("\n");
        }

        if (StrUtil.isNotBlank(m)) {
            i++;
            stringBuilder.append(m).append("\n");
        }

        if (StrUtil.isNotBlank(d)) {
            i++;
            stringBuilder.append(d).append("\n");
        }
        if (i >= 2) {
            stringBuilder.append("\n环境：").append(active);
            stringBuilder.append("\n时间：").append(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            dingDingService.dingMsg(stringBuilder.toString());
        }
        log.info(stringBuilder.toString());

    }

    private String extracted(double cpuSys, String what) {
        StringBuilder stringBuilder = new StringBuilder();

        if (cpuSys > 0.8 && cpuSys < 0.9) {
            String info = what + " 使用率:" + String.format("%.3f", cpuSys) + " 超过:" + 0.8 + " ";
            stringBuilder.append(info);
        } else if (cpuSys > 0.9 && cpuSys < 0.95) {
            String info = what + " 使用率:" + String.format("%.3f", cpuSys) + " 超过:" + 0.9 + " ";
            stringBuilder.append(info);
        } else if (cpuSys > 0.95) {
            String info = what + " 使用率:" + String.format("%.3f", cpuSys) + " 超过:" + 0.95 + " ";
            stringBuilder.append(info);
        }
        return stringBuilder.toString();
    }
}
